import 'dart:io';


import 'dart:math';


import 'package:flutter/widgets.dart';


/// https://medium.com/gskinner-team/flutter-simplify-platform-screen-size-detection-4cb6fc4f7ed1


///


/// bool isLandscape = Screen.isLandscape(context)


/// bool isLargePhone = Screen.diagonal(context) > 720;


/// bool isTablet = Screen.diagonalInches(context) >= 7;


/// bool isNarrow = Screen.widthInches(context) < 3.5;


class Screen {

  static double get _ppi => (Platform.isAndroid || Platform.isIOS) ? 150 : 96;


  static bool isLandscape(BuildContext context) =>

      MediaQuery.of(context).orientation == Orientation.landscape;


  //PIXELS


  static Size size(BuildContext context) => MediaQuery.of(context).size;


  static double width(BuildContext context) => size(context).width;


  static double height(BuildContext context) => size(context).height;


  static double diagonal(BuildContext context) {

    final Size s = size(context);


    return sqrt((s.width * s.width) + (s.height * s.height));

  }


  //INCHES


  static Size inches(BuildContext context) {

    final Size pxSize = size(context);


    return Size(pxSize.width / _ppi, pxSize.height / _ppi);

  }


  static double widthInches(BuildContext context) => inches(context).width;


  static double heightInches(BuildContext context) => inches(context).height;


  static double diagonalInches(BuildContext context) =>

      diagonal(context) / _ppi;


  ///value: 文本内容；fontSize : 文字的大小；fontWeight：文字权重；maxWidth：文本框的最大宽度；maxLines：文本支持最大多少行


  static double calculateTextWidth(BuildContext context, String value,

      FontWeight fontWeight, double fontSize, double maxWidth, int maxLines) {

    if (value.isEmpty) {

      return 0.0;

    }


    TextPainter painter = TextPainter(

      locale: Localizations.localeOf(context),

      maxLines: maxLines,

      textDirection: TextDirection.ltr,

      text: TextSpan(

        text: value,

        style: TextStyle(

          fontSize: fontSize,

          fontWeight: fontWeight,

        ),

      ),

    );


    painter.layout(maxWidth: maxWidth);


    return painter.width;

  }

}


extension MediaQueryExtension on BuildContext {

  Size get size => Screen.size(this);


  double get height => Screen.size(this).height;


  double get width => Screen.size(this).width;

}

